﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
///sqlFormat 的摘要说明
/// </summary>
public class SqlFormat
{
	public SqlFormat()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public static  string ap(string str)
    {
        return "\'" + str + "\'";
    }

    public static  string exeProcedure(string proname,string[] values)
    {
        string sql="exec "+proname;

        for (int i = 0; i < values.Length-1; i++)
        {
            sql = sql + ap(values[i])+",";
        }

        sql = sql + ap(values[values.Length-1])+";";

        return sql;
    }

    public static  string exeProcedureNotap(string proname, string[] values)
    {
        string sql = "exec " + proname;

        for (int i = 0; i < values.Length - 1; i++)
        {
            sql = sql + values[i] + ",";
        }

        sql = sql + values[values.Length - 1] + ";";

        return sql;
    }


    public static string insertTableNotAp(string tableName, string[] values)
    {
        string sql = "insert into "+ tableName +" values (" ;

        for (int i = 0; i < values.Length - 1; i++)
        {
            sql = sql + values[i] + ",";
        }

        sql = sql + values[values.Length - 1] + ");";

        return sql;
    }

    public static string insertTable(string tableName, string[] values)
    {
        string sql = "insert into " + tableName + " values (";

        for (int i = 0; i < values.Length - 1; i++)
        {
            sql = sql + ap(values[i]) + ",";
        }

        sql = sql + ap(values[values.Length - 1]) + ");";

        return sql;
    }

    public static void beginTran(SqlConnection sqlcon)
    {

        SqlDataAdapter sqldapt = new SqlDataAdapter();
        string back = "begin tran;";
        sqldapt.SelectCommand = new SqlCommand(back, sqlcon);
        sqldapt.SelectCommand.ExecuteNonQuery();
    }

    public static void rollback(SqlConnection sqlcon){
        
        SqlDataAdapter sqldapt = new SqlDataAdapter();
           string back = "rollback tran ;";
           sqldapt.SelectCommand = new SqlCommand(back, sqlcon);
           sqldapt.SelectCommand.ExecuteNonQuery();
    }

    public static void commit(SqlConnection sqlcon)
    {

        SqlDataAdapter sqldapt = new SqlDataAdapter();
        string back = "commit tran ;";
        sqldapt.SelectCommand = new SqlCommand(back, sqlcon);
        sqldapt.SelectCommand.ExecuteNonQuery();
    }

    //public static SqlConnection mSqlConnection= new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString());
   
    
}